/*
 * _leave.java
 *
 * Created on 14 Март 2008 г., 13:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package funcs;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.jivesoftware.smackx.muc.MultiUserChat;
import xmppbot.Exec;
import xmppbot.JJConnect;
import xmppbot.Main;

/**
 *
 * @author lian
 */
public class _leave implements func {
    
    /**
     * Creates a new instance of _leave
     */
    public _leave() {
    }

    public String process(Exec exe) {
        String ansver = null;
        ResultSet rs = null;
        String conference = null;
        if (exe.SplitMsg.length == exe.nextArg) {
            exe.mchat.leave();
            ansver = "bye";
        } else {
            try {
                rs = exe.MySQLDB.getStatement().executeQuery("SELECT * FROM conferences WHERE psevdonim = '" + exe.parseSymbols(exe.SplitMsg[exe.nextArg]) + "';");
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            try {
                if (rs.next()) {
                    conference = rs.getString("conference") + "/" + rs.getString("login");
                    ResultSet rs2 = exe.MySQLDB.getStatement().executeQuery("SELECT * FROM servers WHERE id = " + rs.getInt("server") + ";");
                    if (rs2.next()) {
                        String server_name = rs2.getString("user") + "@" + rs2.getString("server") + "/" + rs2.getString("resource");
                        JJConnect server = (JJConnect) Main.Connects.get(server_name);
                        MultiUserChat mchat = (MultiUserChat) server.mchats.get(conference);
                        mchat.leave();
                    }
                } else {
                    ansver = "Bad psevdonim";
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return ansver;
    }
    public String processnotallowed(Exec exe) {
        return null;
    }
}
